package com.fofia.iotweb.web.controllers.admin;

import com.fofia.iotweb.cache.UserCache;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.web.dbdomain.models.admin.SysPosition;
import com.fofia.iotweb.web.dbdomain.services.admin.SysPositionService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;


@Mapping("admin/position")
@Controller
public class PositionController extends BaseController {


    @Inject
    private RequestHelper requestHelper;

    @Inject
    private SysPositionService positionService;
    @Inject
    private UserCache userCache;

    @Get
    @Mapping("index")
    @AuthPermissions("admin.position.view")
    public ModelAndView index(){
        return view();
    }

    @Post
    @Mapping("getPageList")
    @AuthPermissions("admin.position.view")
    public DataTableDto getPageList(DataTableDto dto){

        String postName=requestHelper.getString("postName","");
        DataTableDto data=positionService.getPageList(dto.getPageNumber(),dto.getPageSize(),postName);
        return data;
    }

    @Get
    @Mapping("form")
    @AuthPermissions("admin.position.add;admin.position.edit")
    public ModelAndView form(){
        String id=requestHelper.getString("id","0");
        return view().put("id",id);
    }

    @Get
    @Mapping("getModel")
    @AuthPermissions("admin.position.add;admin.position.edit")
    public ResultAdaptDto getModel(){
        int id=requestHelper.getInt("id",0);
        SysPosition model=positionService.getModel(id);
        ResultAdaptDto ret=JsonResult.success();
        ret.put("model",model);
        return ret;
    }


    @Post
    @Mapping("save")
    @AuthPermissions("admin.position.add;admin.position.edit")
    public ResultAdaptDto save(SysPosition dto){

        positionService.save(dto,userCache.getUserSeesion());
        return JsonResult.success();
    }


    @Get
    @Mapping("removeAll")
    @AuthPermissions("admin.position.delete")
    public ResultAdaptDto removeAll(){
        String ids=requestHelper.getString("ids","0");

        if(ids.isEmpty()||"0".equals(ids)){
            return JsonResult.failure("参数错误");
        }
        positionService.removeAll(ids);
        return JsonResult.success();
    }
}
